package meijuli.com.join51.business.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;
import meijuli.com.join51.business.R;
import meijuli.com.join51.business.activity.BaseFragmentActivity;
import com.jhx.common.http.ApiManager;
import com.jhx.common.http.HttpRequest;
import com.jhx.common.http.HttpResponse;
import com.jhx.common.http.ResponseCallback;
import com.jhx.common.model.EventBean;
import meijuli.com.join51.business.service.ApiService;
import meijuli.com.join51.business.ui.OneKeyDeleteEditView;

import com.jhx.common.model.User;
import com.jhx.common.utils.ToastUtil;
import com.jhx.common.utils.UserManager;

import retrofit2.Call;

/**
 *
 *
 * @author wenqin 2017-07-20 17:13
 */

public class ChangeNameFragment extends BaseFragment {
    private static final String KEY_NAME = "key_name";
    private OneKeyDeleteEditView mNameEdit;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_change_name, container, false);
        User user = UserManager.get().getUser();
        mNameEdit = view.findViewById(R.id.name_edit);
        if(user != null) {
            mNameEdit.setText(user.getBusinessName());
        }
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mNameEdit.setInputType(InputType.TYPE_CLASS_TEXT);
        ((BaseFragmentActivity) mActivity).setToolBarTitle("修改联盟商家名称");
        User user = UserManager.get().getUser();
        if(user != null && !TextUtils.isEmpty(user.getBusinessName())) {
            mNameEdit.setText(user.getBusinessName());
        }
    }

    public void changeNameRequest() {
        if (TextUtils.isEmpty(mNameEdit.getText()) || mNameEdit.getText().trim().length() == 0) {
            ToastUtil.getInstance().showToast("输入信息不能为空");
            return;
        }
        showLoadingDialog();
        Call<HttpResponse<String>> call = ApiManager.get().getApiService(ApiService.class).changeSellerName(mNameEdit.getText());
        HttpRequest.getRequest().autoRecycle(this).start(call, new ResponseCallback() {
            @Override
            public void onSuccess(String url, Object data) {
                dismissDialog();
                EventBus.getDefault().post(new EventBean<>(EventBean.STATUS_MSG));
                mActivity.onBackPressed();
            }

            @Override
            public void onError(String url, int errorCode, String errorMsg) {
                super.onError(url, errorCode, errorMsg);
                dismissDialog();
            }
        });
    }

}
